package com.education.platform.api.controller.notice;

import com.education.platform.api.service.ArticleService;
import com.education.platform.api.service.common.CacheDataService;
import com.education.platform.api.vo.common.BasePageQueryVO;
import com.education.platform.api.vo.response.*;
import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.redis.RedisUtils;
import com.education.platform.common.util.BeanUtil;
import com.education.platform.dao.entity.ArticleDO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
* describe: 文章
* @author : 
* @date: 2020-07-06 09:23:16.228
**/
@RestController
@Api("通知")
@RequestMapping("notice")
@Validated
public class NoticeController {
	
    private static final Logger LOGGER = LoggerFactory.getLogger(NoticeController.class);
    
	@Autowired
    private ArticleService articleService;
	@Autowired
    private RedisUtils redisUtils;
	@Autowired
    private CacheDataService cacheDataService;
    /**
     * 分页查询
     * @param queryVO 分页查询参数
     * @return 分页参数
     */
    @ApiOperation(value = "查询通知")
    @PostMapping("getPageList")
    public NoticePageInfoVO selectPageList(@RequestBody @Valid BasePageQueryVO queryVO) {

        ArticleDO article = new ArticleDO();
        article.setArticleType("NOTICE");
        Page<ArticleResponseVO> page = PageHelper.startPage(queryVO.getPageNum(), queryVO.getPageSize());
        List<ArticleDO> articleList = articleService.getList(article);
        NoticePageInfoVO articlePageInfo = new NoticePageInfoVO();
        BeanUtils.copyProperties(page.toPageInfo(), articlePageInfo);
        List<NoticeResponseVO> voList = BeanUtil.convert(articleList, NoticeResponseVO.class);
        articlePageInfo.setList(voList);
        return articlePageInfo;
    }

    /**
     * 查询通知详情
     * @param
     * @return
     */
    @ApiOperation(value = "查询通知详情")
    @GetMapping("getDetailsById")
    public NoticeDetailResponseVO getDetailsById(@RequestParam Integer id) {

        ArticleDO articleDO = articleService.getById(id);
        if(articleDO == null){
            throw new BusinessException("通知不存在");
        }
        NoticeDetailResponseVO responseVO = BeanUtil.convert(articleDO, NoticeDetailResponseVO.class);
        return responseVO;
    }



}
